package com.lxs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxs.entity.Bill;
import com.lxs.entity.BillType;
import com.lxs.service.BillService;
import com.lxs.service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Aaron
 * @Description:
 * @Date Created in 2021-07-01 21:06
 * @Modified By:
 */
@Controller
@RequestMapping("/bill")
public class BillController {
    @Resource
    private TypeService typeService;
    @Resource
    private BillService billService;
    /**
     * 查询
     * @param b
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Bill b, Model model) {
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        List<Bill> list = billService.list(b);
        model.addAttribute("list", list);
        model.addAttribute("bill", b);
        return "/bill/list";
    }
    /**
            * 调到添加页面
* @return
        */
    @RequestMapping("/toAdd")
    public String toAdd(Model model) {
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        return "/bill/add";
    }
    /**
            * 添加
* @param b
* @return
        */
    @RequestMapping("/add")
    public String add(Bill b) {
        billService.add(b);
        return "redirect:/bill/list";
    }
    /**
            * 删除
* @param id
* @return
        */
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        billService.delete(id);
return "redirect:/bill/list";
}
/**
        * 修改
        * @param id
        * @return
        */
@RequestMapping("/toUpdate/{id}")
public String toUpdate(@PathVariable("id") Long id, Model model) {
        List<BillType> types = typeService.list();
        System.out.println("后台:"+id);
        model.addAttribute("types", types);
        Bill bill = billService.get(id);
        model.addAttribute("bill", bill);
        return "/bill/update";
        }
        /**
        * 修改
        * @param b
        * @return
        */
@RequestMapping("/update")
public String update(Bill b) {
        billService.update(b);
        return "redirect:/bill/list";
        }

    @RequestMapping("/list-page")
    public String listPage(@RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize, Bill b, Model model) {
        System.out.println("进入后台页面");
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        PageInfo<Bill> pageInfo = billService.listPage(b, pageNum, pageSize);
        model.addAttribute("page", pageInfo);
        model.addAttribute("bill", b);

        System.out.println("pageInfo"+pageInfo);
        System.out.println(pageInfo.getList());
        pageInfo.getList().forEach(page-> System.out.println(page));

        return "/bill/list-page";
    }
        }